
<?php
/**
 * class模板
 *
 * PHP version 7.2
 *
 * @category  API
 * @package   YII
 * @author    saruri <saruri@163.com>
 * @copyright 2006-2019 saruri
 * @license   https://saruri.cn/licence.txt BSD Licence
 * @link      http://saruri.cn
 * @date      2021/03/11 14:51:59
 */
//namespace console\models;
//use Yii;
require_once('arr.php');
class CountryMeta
{
    private $_config = [];
    //初始化
    public function __construct()
    {
        //各种配置
        new Terms();
        $re =new Terms();
        $this->countrys=$re->getCountryNameArr()->getTaxonomyArr()->getResult()->run();
        $this->mysql=$re->mysqli;
        $this->metaId=7063;//5566;
        $this->metaId=150044;//五国新增
        $this->urlName='五国新增';
        $this->sql='';
       // var_dump($this->countrys);
        //exit("ces");
    }
      
    /*
    *  @desc      获取新增的 post
    *  @author    saruri <saruri@163.com>
    *  @date      2021/03/11 14:58:49
    */
    public function getPost()
    {
      
   
        //do somehing
        $where = " WHERE meta_id >  ".$this->metaId ." AND meta_key = '_sites_link'";
        $SQL_SELECT_CODE="SELECT * FROM  wp_postmeta".$where;
        $CODE=$this->mysql->query($SQL_SELECT_CODE);
        while ($arr1=$CODE->fetch_array()) {
            $this->postArr[$arr1['meta_id']]=$arr1;
        }
        return $this;
    }


    /*
    *  @desc      gouzao 联系
    *  @author    saruri <saruri@163.com>
    *  @date      2021/03/11 17:36:27
    */
    public function build()
    {
        //do somehing
        $this->errMsg='';
        foreach ($this->postArr as $key => $value) {
            # code...
           
            //$post_id=$value['ID'];
            $arrInsert['meta_id']=$value['meta_id'];
            $arrInsert['link']=$this->addHttp($value['meta_value']);
            //$arrInsert['country']=$value['post_excerpt'];
            $this->sql.=$this->buildInserSql($arrInsert);
        }
        return $this;
    }

    /* 
    *  @desc      补http
    *  @author    saruri <saruri@163.com>
    *  @date      2021/03/12 04:21:57  
    */ 
    public function  addHttp($string)
    {
        //do somehing
        //http://
        if($string==""){
            return '';
        }
        $string=str_replace(" ","",$string);
        $string=str_replace("https://","",$string);
        $string=str_replace("http://","",$string);
        $string='http://'.trim($string);
        return $string;
    }

    /*
    *  @desc      构造插入sql
    *  @author    saruri <saruri@163.com>
    *  @date      2021/03/11 17:26:36
    */
    public function buildInserSql($arr)
    {
        //do somehing
        $SQL_INSERT_CODE='';
        if($arr['meta_id']==''){
            return '';
        }
        $meta_id=$arr['meta_id'];
        $link=$arr['link'];
     
       
      
        $SQL_INSERT_CODE.="UPDATE `wp_postmeta` SET meta_value='$link' WHERE meta_id = $meta_id ;"."\r\n";

        //INSERT INTO `wp_term_relationships` (object_id,term_taxonomy_id) VALUES(1128 ,18); 1127 -> 18 //亚洲的代码  1127 ->211 //不丹的国家代码
        //INSERT INTO `wp_term_relationships` (object_id,term_taxonomy_id) VALUES(1128 ,18);

        //$SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_sites_type','sites');"."\r\n";
        //$SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_sites_link','$link');"."\r\n";
        //$SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_sites_country','$country');"."\r\n";

        //暂时不插入
       // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_edit_lock','1615483336:1');"."\r\n";
        //$SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_edit_last',1);"."\r\n";
        //$SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_wp_old_date','-0001-11-30');"."\r\n";
        //$SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_sites_order','    0');"."\r\n";

        //准备插入
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_visible','');"."\r\n"; 
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_goto','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_is_min_app','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_sites_sescribe','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_sites_language','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_thumbnail','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_wechat_qr','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_down_version','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_down_size','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_dec_password','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_down_preview','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_down_formal','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_sites_screenshot','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_down_url_list','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_app_platform','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_seo_metakey','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_seo_title','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'views','');"."\r\n";
        // $SQL_INSERT_CODE.="INSERT INTO `wp_postmeta` (post_id,meta_key,meta_value) VALUES($post_id,'_seo_desc','');"."\r\n";


        return $SQL_INSERT_CODE;
    }
      
    //保存sql文件
    function save($fileName, $string)
    {  
        $newfolder  = 'c:\excel2';
        $fileName=$fileName.'_'.time();
        $fw = fopen($newfolder.'\\'.$fileName.".sql", "wr");
        //var_dump($fw);
        fputs($fw, $string);
        fclose($fw);
        return $this;
    }

    public function run()
    {
        //主流程
        //var_dump($this->countrys);
        //var_dump($this->postArr);
        //exit($this->errMsg2);
        //exit($this->errMsg);
        $this->save("updateMetaUrl".$this->urlName,$this->sql);
        exit("wancheng ");
        //exit($this->sql);
    }
}


$country = new countryMeta();
$country->getPost()->build()->run();
